using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FireStarterModeller.BO;
using Habanero.Base;

namespace FireStarter.CodeGeneration
{
    public class UIBaseProjectGenerator : ProjectGenerator
    {
        public override Result Generate()
        {
            CheckRequiredPropertiesSet();
            if (ModelSolution.BOProjectName == null) throw new ReferenceMissingException("BOProjectName");
            Directory.CreateDirectory(ProjectPath);

            ProjectFileManager boProjectFileManager = new ProjectFileManager(ModelSolution.BOProjectName, SolutionGenerator.CodeTemplate);

            UiProjectTemplateLoader uiProjectTemplateLoader = new UiProjectTemplateLoader();
            string contents = uiProjectTemplateLoader.GetUiManager(ProjectName, boProjectFileManager.AssemblyName);
            string uiManagerFileName = Utilities.CreateFile(ProjectPath, "UIManager.cs", contents);

            contents = uiProjectTemplateLoader.GetAssemblyInfo(ProjectName);
            Utilities.CreateFile(ProjectPath, "Properties\\AssemblyInfo.cs", contents);


            string projectFileName = Path.Combine(ProjectPath, ProjectName + ".csproj");
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName, OutputType.Library, SolutionGenerator.CodeTemplate);
            projectFileManager.AddCodeFile(uiManagerFileName);
            projectFileManager.AddProjectReference(ModelSolution.BOProjectName, SolutionGenerator.SolutionDirectory);
            if (ModelSolution.LogicProjectName != null)
            {
                projectFileManager.AddProjectReference(ModelSolution.LogicProjectName, SolutionGenerator.SolutionDirectory);
            }
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.BO.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.Base.dll"));
            //projectFileManager.AddReference(Path.Combine(_habaneroReferencePath, "Habanero.Faces.Win.dll"));
            if (GenerateIControlFactory())
                AddDefaultControlFactoryInterface(projectFileManager);

            projectFileManager.SaveProjectFile();
            return new Result(true);
        }

        protected bool GenerateIControlFactory()
        {
            return SolutionGenerator.SolutionTypeName == GenerationTypes.SOLUTION_TYPE_DUALWINVWG;
        }

        private void AddDefaultControlFactoryInterface(ProjectFileManager projectFileManager)
        {
            UiProjectTemplateLoader uiProjectTemplateLoader = new UiProjectTemplateLoader();
            string contents = uiProjectTemplateLoader.GetIControlFactory(ProjectName, SolutionGenerator.ModelSolution);
            string brokerName = Utilities.CreateFile(ProjectPath, "I" + SolutionGenerator.ModelSolution.ApplicationName + "ControlFactory.cs", contents);
            projectFileManager.AddCodeFile(brokerName);
        }

        //private ProjectModelUIBase CreateProjectModelUIBase()
        //{
        //    return new ProjectModelUIBase
        //    {
        //        ProjectName = SolutionGenerator.ModelSolution.ApplicationName,
        //        UIBaseProjectName = ProjectName
        //    };
        //}

        //private class ProjectModelUIBase : IModelElement
        //{
        //    public string ProjectName { get; set; }
        //    public string UIBaseProjectName { get; set; }

        //    #region Implementation of IModelElement

        //    public IModelElement Parent
        //    {
        //        get { return null; }
        //        set { throw new System.NotImplementedException(); }
        //    }

        //    #endregion
        //}
    }
}
